home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2000 September / september_2000.iso / intercd / root / ^Linux / cfengine-1.5.3 / src / encrypt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-08-11  |  4.4 KB  |  183 lines

  1. /* 
  2.  
  3.         Copyright (C) 1999
  4.         Free Software Foundation, Inc.
  5.  
  6.    This file is part of GNU cfengine - written and maintained 
  7.    by Mark Burgess, Dept of Computing and Engineering, Oslo College,
  8.    Dept. of Theoretical physics, University of Oslo
  9.  
  10.    This program is free software; you can redistribute it and/or modify it
  11.    under the terms of the GNU General Public License as published by the
  12.    Free Software Foundation; either version 2, or (at your option) any
  13.    later version.
  14.  
  15.    This program is distributed in the hope that it will be useful,
  16.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18.    GNU General Public License for more details.
  19.  
  20.   You should have received a copy of the GNU General Public License
  21.   along with this program; if not, write to the Free Software
  22.   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
  23.  
  24. */
  25.  
  26.  
  27. /*****************************************************************************/
  28. /*                                                                           */
  29. /* File: encrypt.c                                                           */
  30. /*                                                                           */
  31. /*****************************************************************************/
  32.  
  33. #include "cf.defs.h"
  34. #include "cf.extern.h"
  35. #include "../pub/global.h"
  36. #include "../pub/md5.h"
  37.  
  38.  
  39. #ifdef HAVE_LIBCRYPTO  /* must be des.h from OpenSSL */
  40. # ifdef HAVE_OPENSSL_DES_H
  41. #  include <openssl/des.h>
  42. # endif
  43. #endif
  44.  
  45. /*********************************************************************/
  46.  
  47. LoadSecretKeys()
  48.  
  49. { char filename[maxvarsize];
  50.   struct stat statbuf;
  51.   char seed[8],digest[16];
  52.   FILE *fp;
  53.  
  54. sprintf(filename,"%s/keys",VLOCKDIR);
  55.  
  56. if (stat(filename,&statbuf) == -1)
  57.    {
  58.    return;
  59.    }
  60.  
  61. if (statbuf.st_mode & 077)
  62.    {
  63.    sprintf(OUTPUT,"Secret key file %s has public access! Copying is not secure",filename);
  64.    CfLog(cferror,OUTPUT,"");
  65.    }
  66.  
  67. bzero(CFDES1,8);
  68. bzero(CFDES2,8);
  69. bzero(CFDES3,8);
  70.  
  71. if ((fp = fopen(filename,"r")) == NULL)
  72.    {
  73.    return;
  74.    }
  75.  
  76. fscanf(fp,"%8s%8s%8s",CFDES1,CFDES2,CFDES3);
  77. fclose(fp);
  78.  
  79. Verbose("Loaded DES secret keys from %s\n",filename);
  80.  
  81. #ifdef HAVE_LIBCRYPTO
  82. DESMD5Random(digest);
  83. bcopy(digest,seed,8);
  84. des_random_seed((des_cblock *)seed);
  85. #endif 
  86. }
  87.  
  88. /**************************************************************/
  89.  
  90. DESMD5Random(digest)
  91.  
  92. char digest[16];
  93.  
  94.    /* Make a decent random number by crunching some system states through
  95.       MD5. We can use this as a seed for pseudo random generator */
  96.  
  97. { MD5_CTX context;
  98.   int len;
  99.   unsigned char buffer[bufsize];
  100.   FILE *pp;
  101.   char pscomm[maxlinksize];
  102.  
  103. Verbose("Looking for a good random number seed...\n");
  104. cfMD5Init (&context);
  105. sprintf(buffer,"%d%d",CFSTARTTIME,digest);
  106. cfMD5Update (&context, buffer,bufsize);
  107.  
  108. sprintf(pscomm,"%s %s",VPSCOMM[VSYSTEMHARDCLASS],VPSOPTS[VSYSTEMHARDCLASS]);
  109.  
  110. if ((pp = cfpopen(pscomm,"r")) == NULL)
  111.    {
  112.    sprintf(OUTPUT,"Couldn't open the process list with command %s\n",pscomm);
  113.    CfLog(cferror,OUTPUT,"popen");
  114.    }
  115.  
  116. while (!feof(pp))
  117.    {
  118.    ReadLine(buffer,bufsize,pp);
  119.    cfMD5Update (&context,buffer,bufsize);
  120.    }
  121.  
  122. cfpclose(pp);
  123.  
  124. cfMD5Final (digest, &context);
  125. }
  126.  
  127. /*********************************************************************/
  128.  
  129. #ifdef HAVE_LIBCRYPTO
  130.  
  131. cfencrypt(in,out,key1,key2,key3,len)
  132.  
  133. char *in,*out;
  134. char key1[8],key2[8];
  135. int len;
  136.  
  137. { char workvec[bufsize];
  138.   des_key_schedule ks1,ks2,ks3;
  139.  
  140. des_set_key((C_Block *)key1,ks1);
  141. des_set_key((C_Block *)key2,ks2);
  142. des_set_key((C_Block *)key3,ks3);
  143.  
  144. memset(workvec,0,bufsize);
  145.  
  146. des_ede3_cbc_encrypt(in,out,(long)len,ks1,ks2,ks3,(C_Block *)workvec,DES_ENCRYPT);
  147. }
  148.  
  149. /*********************************************************************/
  150.  
  151. cfdecrypt(in,out,key1,key2,key3,len)
  152.  
  153. char *in,*out;
  154. char key1[8],key2[8],key3[8];
  155. int len;
  156.  
  157. { char workvec[bufsize];
  158.   des_key_schedule ks1,ks2,ks3;
  159.  
  160. memset(workvec,0,bufsize);
  161. des_set_key((C_Block *)key1,ks1);
  162. des_set_key((C_Block *)key2,ks2);
  163. des_set_key((C_Block *)key3,ks3);
  164.  
  165. des_ede3_cbc_encrypt(in,out,(long)len,ks1,ks2,ks3,(C_Block *)workvec,DES_DECRYPT);
  166.  
  167. out[len] = '\0';
  168.  
  169. Debug("Decrypted %d to [%d]\n",len,strlen(out)); 
  170. }
  171.  
  172. #else
  173.  
  174. cfencrypt()
  175. {
  176. }
  177.  
  178. cfdecrypt()
  179. {
  180. }
  181.  
  182. #endif
  183.